﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Data.Common;
using BOHVote.Core;

namespace BOHVote.DAL
{
    public class PeriodProvide
    {
        Database db;
        public PeriodProvide()
        {
            db = DatabaseFactory.CreateDatabase();
        }

        public DataSet getAllProvide(string strStatus, string strApplyPeriodID, string strApplyPeriodName, string ArchivesStatus)
        {
            string strSQL = "SELECT * FROM ApplyPeriod WHERE 1=1 ";
            if (!string.IsNullOrEmpty(strStatus))
            {
                strSQL += " AND status = '" + strStatus + "' ";
            }
            if (!string.IsNullOrEmpty(strApplyPeriodName))
            {
                strSQL += " AND ApplyPeriodNam like '%" + strApplyPeriodName + "%' ";
            }
            if (!string.IsNullOrEmpty(ArchivesStatus))
            {
                strSQL += " AND ArchivesStatus = '" + ArchivesStatus + "' ";
            }
            if (!string.IsNullOrEmpty(strApplyPeriodID))
            {
                strSQL += " AND ApplyPeriodID = '" + strApplyPeriodID + "' ";
            }
            strSQL += " ORDER BY ApplyPeriodID ASC";

            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteDataSet(db, dbcommand);
        }

        public int DeletePeriodByID(string strPeriodID)
        {
            string strSQL = " DELETE FROM ApplyPeriod WHERE ApplyPeriodID = '" + strPeriodID + "' ";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteNonQuery(db, dbcommand);
        }

        public int UpdatePeriod(string ApplyPeriodID,string DateFrom, string ApplyPeriodName, string DateTo, string ArchivesStatus, string DatachangedUserID,string status)
        {
            string strSQL = " UPDATE ApplyPeriod SET DateFrom = '" + DateFrom + "',ApplyPeriodName='" + ApplyPeriodName + "',DateTo='" + DateTo + "',ArchivesStatus='" + ArchivesStatus + "',DatachangedUserID='" + DatachangedUserID + "',DataChangedDate = getdate(),status = '"+status+"' WHERE ApplyPeriodID = '"+ApplyPeriodID+"' ";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteNonQuery(db, dbcommand);
        }

        public int InsertPeriod(string ApplyPeriodID, string DateFrom, string ApplyPeriodName, string DateTo, string ArchivesStatus, string DataEnteredUserID,string status)
        {
            string strSQL = "INSERT INTO ApplyPeriod(ApplyPeriodID,DateFrom,ApplyPeriodName,DateTo,ArchivesStatus,DataEnteredUserID,DataEnteredDate,status)VALUES('"+ApplyPeriodID+"','"+DateFrom+"','"+ApplyPeriodName+"','"+DateTo+"','"+ArchivesStatus+"','"+DataEnteredUserID+"',getdate(),'"+status+"')";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteNonQuery(db, dbcommand);
        }
    }
}
